const Controllers = require('../controllers/Controller.js').controller;
const app = getApp();
const util = require('../../utils/util.js');
Page({
  data: {
    globalStype:'',
    isShow:false,
    isShow2:false,
    goodsId:'',
    goodsDetailsdata:'',
    time1:'',
    time2:'',
    maxtime:'21:00',
    mintime:"09:00",
    middletime:'21:00',
    time:'',
    type:'',
    remask:'',
    // timeattry:['9:30 - 12:00','14:00-18:00','19:00-22:00'],
    timeattry:['2020-10-28 08:00','2020-10-28 18:00'],
    timeindex:-1,
    typeattry:['推拿忙内（职称：学徒级别）','推拿小能手（职称：新手级别）','推拿大师（职称：大师级别）'],
    typeindex:-1,
    nick:'',
    shopStaffdata:'',
    orderId:'',
    date:'',
    currentDate:util.formatTime_2(new Date()),
  },

  onLoad(option) {
    this.setData({globalStype:app.globalData.globalStype})
    console.log(option);
    
    // if(option.id || option.goodsid){
    //   this.setData({
    //     goodsId:option.id || option.goodsid
    //   })
    //   this.goodsDetails()
    // }
    if(app.globalData.shopId){
      this.setData({
        shopId:app.globalData.shopId
      })
    }
    if(option.orderId){
      this.setData({
        orderId:option.orderId
      })
    }
    if(option.orderNo){
      this.setData({
        orderNo:option.orderNo
      })
    }
    this.getStaffList()
  },
  // selectservice() {
  //   this.setData({
  //     isShow2: true
  //   })
  // },
  // closeservice() {
  //   this.setData({
  //     isShow2: false
  //   })
  // },
  selecttime() {
    this.setData({
      isShow: true
    })
  },
  closetime() {
    this.setData({
      isShow: false
    })
  },
  changeremask(e){
    this.setData({
      remask:e.detail.value
    })
  },

  //跳转预约记录
  toRecond(){
    wx.navigateTo({
      url: '/pages/my/appointment-record/appointment-record',
    })
  },
  
  goodsDetails(){
    //获取数据
    Controllers.goodsDetails({goodsId:this.data.goodsId}).then(res => {
      // res.data.rotationImgs=res.data.rotationImgs.split('/')
      if(res.data.labelName)
      res.data.labelName=res.data.labelName.split('/')
      // res.data.detailImgs=res.data.detailImgs.split('/')
      this.setData({
        goodsDetailsdata:res.data
      })
      this.shopStaff()
    })
  },
  gettime(e){
    let index=e.currentTarget.dataset.index;
    let value=e.currentTarget.dataset.time;
    this.setData({
      timeindex:index,
      time:value
    })
  },
  changeDateTime2(e) {
    var time=e.detail.value
     this.setData({
      date: time,
     });
  },
  // gettype(e){
  //   let index=e.currentTarget.dataset.index;
  //   let value=e.currentTarget.dataset.type;
  //   let nick=e.currentTarget.dataset.nick;
  //   this.setData({
  //     typeindex:index,
  //     type:value,
  //     nick:nick
  //   })
  // },
  getSeconds(time){
    var time1=Number(time.split(':')[0])*60
    var time2=Number(time.split(':')[1])
    return time1+time2
  },
  comfirmservice(){

    let data={}
    // data.goodsId=this.data.goodsId;
    data.shopId=this.data.shopId;
    data.shopAccountId=this.data.staffId;
    data.desc=this.data.remask;

    data.agreeTime=this.data.date +' '+ this.data.time
    let data1={}
    data1.shopId=this.data.shopId;
    data1.shopAccountId=this.data.staffId;
    data1.desc=this.data.remask;

    data1.agreeTime=this.data.date +' '+ this.data.time
    let nowTime=new Date().getTime();
    let agreeTime=new Date(data.agreeTime.replace(/-/,'/')).getTime()
    let agreeTime1=new Date(data1.agreeTime.replace(/-/,'/')).getTime()
    if(agreeTime<nowTime||agreeTime1<nowTime){
      wx.showToast({
        title: '不能预约过去的时间',
        icon:'none'
      })
      return
    }
    if(!this.data.date){
      wx.showToast({
        title: '请选择预约日期',
        icon: 'none'
      })
      return
    }

    if(!this.data.time1){
      wx.showToast({
        title: '请选择预约开始时间',
        icon: 'none'
      })
      return
    }
    // if(!this.data.time2){
    //   wx.showToast({
    //     title: '请选择预约结束时间',
    //     icon: 'none'
    //   })
    //   return
    // }
    // if(this.getSeconds(this.data.time1)>this.getSeconds(this.data.time2)){
    //   wx.showToast({
    //     title: '开始时间不能大于结束时间',
    //     icon: 'none'
    //   })
    //   return
    // }


    if(this.data.orderId){
      let that=this;
      data1.orderId=this.data.orderId
      Controllers.agreePayOrder(data1).then(res => {
        wx.showToast({
          title: '预约成功！',
          icon: 'none'
        })
        setTimeout(res=>{
          wx.navigateBack()
        },1000)
      })
    }else{
      let that=this;
      Controllers.agreeService(data).then(res => {
        wx.showToast({
          title: '预约成功！',
          icon: 'none'
        })
        setTimeout(res=>{
          // wx.navigateTo({
          //   url:'/pages/my/appointment-record/appointment-record'
          // })
          // wx.redirectTo({
          //   url:'/pages/Index/Index'
          // })
          // wx.navigateTo({
          //   url: '/pages/my/serviceOrder-detail/serviceOrder-detail?id='+that.data.orderNo,
          // })
          wx.reLaunch({
            url: '/pages/my/my-index/my-index',
          })
        },1000)
      })
    }
  },
  shopStaff(){
    let data={}
    Controllers.shopStaff({shopId:this.data.shopId}).then(res => {
      this.setData({
        shopStaffdata:res.data
      })
    })
  },
  bindTimeChange1: function(e) {
    this.setData({
      time1: e.detail.value,
      middletime:e.detail.value,
      time:e.detail.value
    })
  },
  // bindTimeChange2: function(e) {
  //   this.setData({
  //     time2: e.detail.value,
  //     middletime:e.detail.value,
  //     time:this.data.time1+'-'+e.detail.value
  //   })
  // },

  //2021/04/13修改

  getStaffList(){
    let shopId=wx.getStorageSync('shopId')
    Controllers.getStaffList({shopId:shopId}).then(res=>{
      this.setData({
        list:res.data
      })
    })
  },
  //关闭员工选框
  closeOpen(){
    this.setData({
      staffBox:false
    })
  },

  //显示弹框
  showModal: function() {
    this.setData({
      showModalStatus: true
    })
  },
  // 隐藏弹框
  hideModal: function() {
      this.setData({
        showModalStatus: false
      })
  },

  //选择员工
  selectStaff(e){
    let index=e.currentTarget.dataset.index
    console.log(e)
    if(index===this.data.selectIndex){
      this.setData({
        selectIndex:"",
        staffId:"",
        staffName:""
      })
    }else{
      this.setData({
        selectIndex:index
      })
    }
  },

  //确定员工
  defineStaff(){
    if(this.data.selectIndex||this.data.selectIndex===0){
      this.setData({
        staffId:this.data.list[this.data.selectIndex].id,
        staffName:this.data.list[this.data.selectIndex].nick
      })
    }
    this.hideModal()
  },

  //跳转员工详情
  selectStaffDetail(e){
    let id=e.currentTarget.dataset.id
    let index=e.currentTarget.dataset.index
    let value=this.data.list[index]
    wx.navigateTo({
      url: '/pages/selecStaffDetail/selecStaffDetail?staffDetail='+encodeURIComponent(JSON.stringify(value)),
    })
  }
})